package com.deuwise.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.deuwise.common.utils.R;
import com.deuwise.common.utils.Result;
import com.deuwise.common.validator.ValidatorUtils;
import com.deuwise.common.validator.group.AddGroup;
import com.deuwise.common.validator.group.UpdateGroup;
import com.deuwise.system.entity.LayerTypeDo;
import com.deuwise.system.entity.SysRole;
import com.deuwise.system.service.LayerTypeDoService;
import com.deuwise.system.service.SysRoleMenuService;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *@author deuwise
 *@date 2018-11-06 14:15:04
 */
@Api(tags = "图层类型模块", description = "图层类型模块")
@RequestMapping("api/sys/layerType")
@RestController()
public class LayerTypeDoController extends  AbstractController {
    @Autowired
    private LayerTypeDoService layerTypeDoService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    /**
     * 角色列表
     */
    @ApiOperation("获取图层类型list")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "currentPage", value = "开始页数", defaultValue = "1", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页多少条", defaultValue = "15", dataType = "int", paramType = "query")})
    public Result<Page<LayerTypeDo>> list(@RequestParam(value = "name",required = false) String name) {

        Wrapper<LayerTypeDo> wrapper = new EntityWrapper<>();
        if (name!=null&&StringUtils.isNotBlank(name)) {
            wrapper.like("name", name);
        }
        wrapper.orderBy("sort",true);
        Page<LayerTypeDo> page = layerTypeDoService.selectPage(getPage(LayerTypeDo.class), wrapper);

        return Result.ok(page);
    }


    /**
     * 图层类型信息
     */
    @ApiOperation("图层类型详情")
    @GetMapping("/{roleId}")
    public R info(@PathVariable("roleId") String roleId) {
        LayerTypeDo role = layerTypeDoService.selectById(roleId);

        return R.ok().put("role", role);
    }

    /**
     * 保存图层
     */
    @ApiImplicitParam(name = "layerTypeDo", value = "图层类型对象", required = true, dataType = "LayerTypeDo", paramType = "body")
    @ApiOperation("新增图层类型")
    @PostMapping("")
    public R save(@RequestBody LayerTypeDo layerTypeDo) {
        layerTypeDo.setId(null);
        ValidatorUtils.validateEntity(layerTypeDo, AddGroup.class);
        int count = layerTypeDoService.queryByName(layerTypeDo.getName(),null);
        if (count > 0) {
            return R.error(200, "图层类型名称不能重复");
        }
        layerTypeDoService.insert(layerTypeDo);
        return R.ok();
    }

    /**
     * 修改图层
     */
    @ApiOperation("修改图层")
    @PutMapping("")
    @ApiImplicitParam(name = "layerTypeDo", value = "图层对象", required = true, dataType = "LayerTypeDo", paramType = "body")
    public R update(@RequestBody LayerTypeDo layerTypeDo) {
        ValidatorUtils.validateEntity(layerTypeDo, UpdateGroup.class);
        int count = layerTypeDoService.queryByName(layerTypeDo.getName(),layerTypeDo);
        if (count > 0) {
            return R.error(200, "图层类型称不能重复");
        }
        layerTypeDoService.updateById(layerTypeDo);

        return R.ok();
    }

    @ApiOperation(value = "删除图层类型")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id") String id) {
        try {
            layerTypeDoService.deleteById(id);

        } catch (Exception ex) {
            return R.error("删除失败");
        }
        return R.ok();
    }


    @ApiOperation(value = "批量删除记录")
    @RequiresPermissions("sys:role:delete")
    @RequestMapping(value = "/deleteSelect", method = RequestMethod.POST)
    public  R deleteSelect(@ApiParam(value = "ids集合",name = "ids",required = true) @RequestParam String[] ids){
        try {
            if (ids != null && ids.length > 0) {
                layerTypeDoService.deleteBatchIds(Arrays.asList(ids));
            }
        }catch (Exception ex){
            return  R.error("删除失败");
        }
        return  R.ok();
    }

}